package cn.wolfcode.luowowo.service.impl;


import cn.wolfcode.luowowo.domain.*;
import cn.wolfcode.luowowo.query.StrategyObjcet;
import cn.wolfcode.luowowo.repository.RegionRepository;
import cn.wolfcode.luowowo.repository.StrategyRepository;
import cn.wolfcode.luowowo.service.*;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyServiceImpl implements IStrategyService {
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private StrategyRepository strategyRepository;
    @Autowired
    private IDestinationService destinationService;
    @Override
    public Strategy get(String id) {
        return  strategyRepository.findById(id).orElse(null);
    }

    @Override
    public List<Strategy> list() {
        List<Strategy> list = strategyRepository.findAll();
        return list;
    }



    @Override
    public Page<Strategy> query(StrategyObjcet qo) {
        //创建查询对象
        Query query = new Query();
       if(StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
       }
        //分页查询

        PageRequest request = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        //关键字查询
        if(StringUtils.hasLength(qo.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(".*"+qo.getKeyword()+".*"));
        }
       return DBHelper.query(template,Strategy.class,query,request);
    }


    @Override
    public void saveOrUpdate(Strategy strategy) {
            StrategyCatalog catalog = strategyCatalogService.get(strategy.getCatalogId());
            strategy.setDestName(catalog.getDestName());
            strategy.setDestId(catalog.getDestId());
            strategy.setCatalogName(catalog.getName());
            StrategyTheme theme = strategyThemeService.get( strategy.getThemeId());
            strategy.setThemeName(theme.getName());
            //是否国外
              List<Destination> toasts = destinationService.getToasts(catalog.getDestId());
             if(toasts!=null && toasts.size()>0){
                   strategy.setIsabroad("中国".equals(toasts.get(0).getName())?Strategy.ABROAD_NO:Strategy.ABROAD_YES);
            }
            if(StringUtils.hasLength(strategy.getId())){
                this.update(strategy);
                Query query =new Query();
                query.addCriteria(Criteria.where("_id").is(strategy.getId()));
                DBHelper.update(template,query,strategy,"destId","destName","themeId","themeName","catalogId","catalogName","title","subTitle","summary","coverUrl","isabroad","state","content");
            }else{
                strategy.setCreateTime(new Date());
                this.save(strategy);
            }


    }

    @Override
    public void delete(String id) {
        strategyRepository.deleteById(id);
    }

    @Override
    public void save(Strategy strategy) {
        strategy.setId(null);
        strategyRepository.save(strategy);
    }

    @Override
    public void update(Strategy strategy) {

        strategyRepository.save(strategy);
    }

    @Override
    public void changeState(int state, String id) {
        Query query = new Query();
        //根据ID选出哪一个ID需要变动
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.set("state",state);
        template.updateMulti(query, update, Strategy.class);
    }

    @Override
    public List<Strategy> queryCatalogId(String catalogId) {
        return strategyRepository.findByCatalogId(catalogId);
    }

    @Override
    public List<Strategy> getStrategyByCatalogId(String id) {
        return strategyRepository.findByCatalogId(id);
    }

    @Override
    public List<Strategy> viewnumTop3(String destId) {
        PageRequest request = PageRequest.of(0, 3, Sort.Direction.DESC,"viewnum");
        return strategyRepository.findByDestId(destId,request);
    }




}
